﻿using CommonTools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StationDemo
{
    public class TableData
    {     
        public int StationIndex { get; set; }
        public bool StartSingle { get; set; }
        public bool FinishSingle { get; set; }
        
    }
    public class TableDataMgr
    {

        public static object _lock = new object();
        public static TableDataMgr tableData = null;
        public static TableDataMgr GetInstance()
        {

            lock (_lock)
            {
                if (tableData == null)
                {
                    lock (_lock)
                    {
                        tableData = new TableDataMgr();

                    }

                }
                return tableData;
            }

        }



        public TableData[] tableDatas = new TableData[4] { new TableData(), new TableData(), new TableData(), new TableData() };




       

        #region 交互信号

        public void TableDataInit()
        {
            tableDatas[0].StationIndex = (int)Station.上料站;
            tableDatas[1].StationIndex = (int)Station.右图卡站;
            tableDatas[2].StationIndex = (int)Station.中间图卡站;
            tableDatas[3].StationIndex = (int)Station.左图卡站;
        }



        /// <summary>
        /// 复位所有工站为未开始，已完成状态
        /// </summary>
        /// <returns></returns>
        public void Func_ResetRunState()
        {
            foreach(var item in tableDatas)
            {
                item.FinishSingle = true;
                item.StartSingle = false;
            }
        }

        /// <summary>
        /// 复位所有工站为未完成状态
        /// </summary>
        /// <returns></returns>
        public void Func_ResetAllStationFinishState()
        {
            foreach (var item in tableDatas)
            {
                item.FinishSingle = false;
                
            }
        }

        /// <summary>
        /// 设置所有工站开始测试
        /// </summary>
        public void Func_SetAllStationStartState()
        {
            foreach (var item in tableDatas)
            {
                item.StartSingle = true;

            }
        }
        /// <summary>
        /// 设置当前工站完成状态
        /// </summary>
        /// <returns></returns>
        public void Func_SetFinishState(int Index)
        {
            foreach (var item in tableDatas)
            {
                if (item.StationIndex == Index)
                {
                    item.FinishSingle = true;
                }
               

            }
        }
        /// <summary>
        /// 复位当前工站开始信号
        /// </summary>
        /// <param name="Index"></param>
        public void Func_ResetStartSingal(int Index)
        {
            foreach (var item in tableDatas)
            {
                if (item.StationIndex == Index)
                {
                    item.StartSingle = false;
                }


            }
        }

        /// <summary>
        /// 获取所有站是否完成
        /// </summary>
        /// <returns></returns>
        public bool Func_GetAllStationFinishState()
        {
            bool re = true;
            foreach (var item in tableDatas)
            {
                re &= item.FinishSingle;
                re &= !item.StartSingle;

            }
            return re;
        }
        
        /// <summary>
        /// 获取开始信号
        /// </summary>
        /// <param name="Index"></param>
        /// <returns></returns>
        public bool Func_GetStartSingal(int Index)
        {
           
            foreach (var item in tableDatas)
            {
                if (item.StationIndex == Index)
                {
                    return item.StartSingle;
                }


            }
            return false;
        }

        #endregion


    }
}
